/*
 * Copyright (C) 2009  Sylvain Maucourt (smaucourt@gmail.com)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 *
 */
package net.sylvek.where;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;

public class UploadPhoto extends DlgActivity {

    @Override
    protected Dialog onCreateDialog(int id)
    {
        switch (id) {
        default:
            return super.onCreateDialog(id);
        case ASK_DLG:
            View view = LayoutInflater.from(this).inflate(R.layout.comment_box, null);

            final EditText c = (EditText) view.findViewById(R.id.status_text);
            final CheckBox loc = (CheckBox) view.findViewById(R.id.add_location);

            return new AlertDialog.Builder(this).setTitle(R.string.upload_photo_question)
                    .setCancelable(false)
                    .setView(view)
                    .setPositiveButton(android.R.string.yes, new OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which)
                        {
                            Intent uploadPhoto = new Intent(getApplicationContext(), UploadPhotoService.class);
                            uploadPhoto.setData(getData());
                            uploadPhoto.putExtra(UploadPhotoService.PHOTO_TEXT, c.getText().toString());
                            uploadPhoto.putExtra(UploadPhotoService.PHOTO_LOCALIZED, loc.isChecked());
                            startService(uploadPhoto);
                            handler.post(new Runnable() {

                                @Override
                                public void run()
                                {
                                    finish();
                                }
                            });
                        }
                    })
                    .setNegativeButton(android.R.string.no, new OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which)
                        {
                            finish();
                        }
                    })
                    .create();
        }
    }

    private Uri getData()
    {
        Bundle extras = getIntent().getExtras();
        return (Uri) extras.get(Intent.EXTRA_STREAM);
    }

}
